package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.log.LogUnionChamberDispatch;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = Protocol.S_UNION_CHAMBER_DISPATCH, desc = "商会议事厅派遣")
public class UnionChamberDispatchCmd extends AbstractCommand {

	private int patronsId;
	boolean isNotify = false;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionChamberDispatchReqMsg reqMsg = UnionProto.UnionChamberDispatchReqMsg.parseFrom(packet.getBytes());
		patronsId = reqMsg.getPatronsId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}

		if (StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}

		UnionInfo unionInfo = UnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
		if(null == unionInfo || unionInfo.isDelete()){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
		if(null == userPatrons){
			ret = GameErrorCode.E_PATRONS_NO_FOUND;
			return;
		}
		UnionMember unionMember = UnionMgr.getUnionMember(player.getUserInfo().getUnionUid(), player.getUserId());
		if(null == unionMember){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		int oldPatronsId = 0;
		synchronized (unionInfo) {
			PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(patronsId);
			int count = UnionConfigMgr.getUnionChamberOccupationPatronsCount(patronsInfo.getOccupation(), unionInfo.getUnionUid(), player.getUserId());
			if(count >= GameConfig.UNION_CHAMBER_PATRONS_LIMIT){
				ret = GameErrorCode.E_UNION_CHAMBER_PATRONS_OVER_COUNT;
				return;
			}
			if (unionMember.getChamberPatronsId() != patronsId) {
				oldPatronsId = unionMember.getChamberPatronsId();
				unionMember.setChamberPatronsId(patronsId);
				unionMember.setChamberPatron(userPatrons);
				isNotify = true;
			}
		}

		//DataAnalyticsMgr.trackOreWarAssemblyhall(player,UnionMgr.getChamberPatronsTotalEarnSpeed(unionInfo));

		if(oldPatronsId > 0){
			//撤回
			LogMgr.addLogUnionChamberDispatch(new LogUnionChamberDispatch(player.getUserId(), unionInfo.getUnionUid(), 0, oldPatronsId, System.currentTimeMillis()));
		}
		//派遣
		LogMgr.addLogUnionChamberDispatch(new LogUnionChamberDispatch(player.getUserId(), unionInfo.getUnionUid(), 1, patronsId, System.currentTimeMillis()));
	}

	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionChamberDispatchRespMsg.Builder respMsg = UnionProto.UnionChamberDispatchRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_UNION_CHAMBER_DISPATCH, respMsg);
		if(isNotify){
			UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
			UnionMgr.notifyAllMemberChamberChange(player.getUserInfo().getUnionUid(), player.getUserId(), false, userPatrons);
		}
	}

}
